function [x,y,Done] = borders( M )
% x, y are column vectors of coordinates of points on border of logical matrix M
%UNTITLED1 Summary of this function goes here
%  Detailed explanation goes here
maxpos = 1000;
points = zeros(maxpos,2);       %prepare memory
curpos = 1;

Done = zeros(size(M));
Done = Done > 0;

for j = 1:size(M,2)
    for i = 1: size(M,1)
        if M(i,j)
            if ~Done(i,j)
                points(curpos,1) = j;
                points(curpos,2) = i;
                curpos = curpos +1;
                if curpos == maxpos         %add more memory
                    points = [points zeros(maxpos,2)]
                    maxpos = maxpos * 2;
                end;
            end
            Done(i,j) = 1;
            break;
        end;
    end;
end;

for j2 = 1:size(M,2)
    for i2 =  size(M,1):-1:1
        if M(i2,j2)
            if ~Done(i2,j2)
                points(curpos,1) = j2;
                points(curpos,2) = i2;
                curpos = curpos +1;
                if curpos == maxpos         %add more memory
                    points = [points zeros(maxpos,2)]
                    maxpos = maxpos * 2;
                end;
            end
            Done(i2,j2) = 1;
            break;
        end;
    end;
end;

for i3 = 1: size(M,1)
    for j3 = 1:size(M,2)
        if M(i3,j3)
            if ~Done(i3,j3)
                points(curpos,1) = j3;
                points(curpos,2) = i3;
                curpos = curpos +1;
                if curpos == maxpos         %add more memory
                    points = [points zeros(maxpos,2)]
                    maxpos = maxpos * 2;
                end;
            end
            Done(i3,j3) = 1;
            break;
        end;
    end;
    for j4 = size(M,2):-1:1
        if M(i3,j4)
            if ~Done(i3,j4)
                points(curpos,1) = j4;
                points(curpos,2) = i3;
                curpos = curpos +1;
                if curpos == maxpos         %add more memory
                    points = [points zeros(maxpos,2)]
                    maxpos = maxpos * 2;
                end;
            end
            Done(i3,j4) = 1;
            break;
        end;
    end;
end;
x = points(1:(curpos - 1),1);
y = points(1:(curpos - 1),2);


